import api from "@/api"
import {transformRoute} from '@/utils/authUtil'
export default{
    namespaced:true,
    state:{
        menus:[],
        isCollapse:false, //默认设置没有折叠
        activePath:''
    },
    mutations:{
        SET_MENUS(state,payload){
            state.menus=payload
        },
        SET_COLLAPSE(state,payload){
            state.isCollapse=payload
        },
        SET_ACTIVE_PATH(state,payload){
            console.log('-------调用集合方法-----------');
            state.activePath=payload
        }
    },
    actions:{
        async getAuthMenusAsync(cxt){
            console.log('------actions的getAuthMenusAsync----------');
            const result=await api.loginModule.getAuthMenusApi()
            cxt.commit('SET_MENUS',result.data)
            /**
             * 下面代码是用来根据后端的权限数据来动态的生成路由配置对象的
             */
            console.log('权限数据',result.data);
            let ary=[]
            transformRoute(result.data,ary)
            let children=ary.map(item=>{
                return{
                    path:item.component.substring(1),
                    component:()=>import(`@/views${item.component}.vue`)
                }
            })
            let homeRoute={
                path:'/',
                component:()=>import('@/views/HomeView.vue'),
                children:children
            }
            return homeRoute
        }
    }
}